﻿using System;
using System.Diagnostics;
using System.Windows.Controls;
using System.Windows.Data;

namespace Flare.Pages.Hards
{
    public partial class HardModel : UserControl, ITypicalPage
    {
        public string name
        {
            get { return "Модели оборудования"; }
        }

        public HardModel()
        {
            InitializeComponent();
        }

        private void ModelList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (ModelList.SelectedIndex == -1)
            {
                ButtonSave.Visibility = System.Windows.Visibility.Collapsed;
                ButtonDelete.Visibility = System.Windows.Visibility.Collapsed;
                ButtonClose.Visibility = System.Windows.Visibility.Collapsed;
                ButtonAdd.Visibility = System.Windows.Visibility.Visible;
            }
            else
            {
                var current_el = (Flare.DB.Entity.EquipmentModelAdvance)ModelList.SelectedItem;
                techType.SelectedValue = current_el.id_type;
                ((ObjectDataProvider)this.Resources["equipment_marksDataSet"]).MethodParameters[0] = (int)techType.SelectedValue;
                techMark.SelectedValue = current_el.id_mark;

                ButtonSave.Visibility = System.Windows.Visibility.Visible;
                ButtonDelete.Visibility = System.Windows.Visibility.Visible;
                ButtonClose.Visibility = System.Windows.Visibility.Visible;
                ButtonAdd.Visibility = System.Windows.Visibility.Collapsed;
            }
        }

        private void techType_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ((ObjectDataProvider)this.Resources["equipment_marksDataSet"]).MethodParameters[0] = (int?)techType.SelectedValue;
            if (ModelList.SelectedIndex == -1)
            {
                ((ObjectDataProvider)this.Resources["equipment_modelsDataSet"]).MethodParameters[0] = (int?)techType.SelectedValue;
            }
        }

        private void techMark_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (ModelList.SelectedIndex == -1)
            {
                ((ObjectDataProvider)this.Resources["equipment_modelsDataSet"]).MethodParameters[1] = (int?)techMark.SelectedValue;
            }
        }

        private void ButtonAdd_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            var data = new Flare.DB.Entity.EquipmentModel()
            {
                model = techModel.Text,
                id_mark = (int?)techMark.SelectedValue
            };

            try
            {
                new DB.equipment_models().Create(data);
                techModel.Text = string.Empty;
            }
            catch (Exception ex)
            {
                glob.kernel.ShowNotice(ex.Message);
            }

            glob.kernel.RefreshResourceByName("equipment_modelsDataSet");
        }

        private void ButtonSave_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            var data = new Flare.DB.Entity.EquipmentModel()
            {
                model = techModel.Text,
                id_model = (int?)ModelList.SelectedValue,
                id_mark = (int?)techMark.SelectedValue
            };

            try
            {
                new DB.equipment_models().Edit(data);
                techType.SelectedIndex = -1;
                ModelList.SelectedValue = -1;
                techModel.Text = string.Empty;
            }
            catch (Exception ex)
            {
                glob.kernel.ShowNotice(ex.Message);
            }

            glob.kernel.RefreshResourceByName("equipment_modelsDataSet");
        }

        private void ButtonDelete_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            if (ModelList.SelectedValue == null)
            {
                glob.kernel.ShowNotice("Значение [Модель оборудования] должно иметь значение");
                return;
            }

            int id_model = (int)ModelList.SelectedValue;

            try
            {
                new DB.equipment_models().Delete(id_model);
                techType.SelectedIndex = -1;
                ModelList.SelectedValue = -1;
                techModel.Text = string.Empty;
            }
            catch (Exception ex)
            {
                glob.kernel.ShowNotice(ex.Message);
            }

            glob.kernel.RefreshResourceByName("equipment_modelsDataSet");
        }

        private void ButtonClose_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            techType.SelectedIndex = -1;
            ModelList.SelectedValue = -1;
        }
    }
}
